package com.x.erp.dao;

import com.x.erp.model.InsuranceGoods;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 延保商品dao
 *
 * @author pansong
 */
@Repository
public class InsuranceGoodsDao extends BaseDao {

	/**
	 * 获取延保商品列表
	 *
	 * @param insuranceGoods
	 * @return
	 */
	private final static String selectInsuranceGoodsListSql = "SELECT eig.* FROM ERP_INSURANCE_GOODS eig "
			+ "WHERE 1 = 1 ";

	public List<InsuranceGoods> selectInsuranceGoodsList(InsuranceGoods insuranceGoods) throws Exception {
		StringBuilder sql = new StringBuilder(selectInsuranceGoodsListSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceGoods);
		return this.queryForList(sql.toString(), InsuranceGoods.class, params, insuranceGoods);
	}

	/**
	 * 获取单个延保商品信息
	 *
	 * @param insuranceGoods
	 * @return
	 */
	private final static String selectInsuranceGoodsSql = "SELECT eig.* FROM ERP_INSURANCE_GOODS eig "
			+ "WHERE 1 = 1 ";

	public InsuranceGoods selectInsuranceGoods(InsuranceGoods insuranceGoods) throws Exception {
		StringBuilder sql = new StringBuilder(selectInsuranceGoodsSql);
		List<Object> params = new ArrayList<>();
		selectSearch(sql, params, insuranceGoods);
		sql.append("LIMIT 1 ");
		List<InsuranceGoods> goodsList = this.queryForList(sql.toString(), InsuranceGoods.class, params);
		if (goodsList != null && goodsList.size() > 0) {
			return goodsList.get(0);
		}
		return null;
	}

	/**
	 * 拼接查询条件
	 */
	private void selectSearch(StringBuilder sql, List<Object> params, InsuranceGoods insuranceGoods) {
		if (this.isNotNull(insuranceGoods.getGoodsId())) {
			sql.append("AND eig.GOODS_ID = ? ");
			params.add(insuranceGoods.getGoodsId());
		}
		if (this.isNotNull(insuranceGoods.getGoodsName())) {
			sql.append("AND eig.GOODS_NAME = ? ");
			params.add(insuranceGoods.getGoodsName());
		}
		if (this.isNotNull(insuranceGoods.getGoodsAdvicePrice())) {
			sql.append("AND eig.GOODS_ADVICE_PRICE = ? ");
			params.add(insuranceGoods.getGoodsAdvicePrice());
		}
		if (this.isNotNull(insuranceGoods.getGoodsBasePrice())) {
			sql.append("AND eig.GOODS_BASE_PRICE = ? ");
			params.add(insuranceGoods.getGoodsBasePrice());
		}
	}
}
